﻿#region File Description
///------------------------------------------------------------------
/// Program		  :	Algorithm With Graph
/// Written by	  :	Nguyen Khac Trung, Le Hoang Tuan
/// Email		  :	nkt1321991@gmail.com || hoangtuanuit@gmail.com
/// File Name     : MyPathList.cs
/// Created date  :	10/11/2011
/// Modified by	  :	Nguyen Khac Trung
/// Modified date :	11/11/2011
/// Version		  :	1.2
/// Description	  :	Declare my class Path
///------------------------------------------------------------------
#endregion 

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
#endregion

namespace Graph
{
    /// <summary>
    /// 
    /// </summary>
    public class MyPath
    {

        #region Fields

        private Path m_myPath;

        private String m_nameFirstPoint;

        private String m_nameEndPoint;
        #endregion

        #region Properties

        public Path MYPATH
        {
            get { return m_myPath; }
            set { m_myPath = value; }
        }

        public String NAMEFIRSTPOINT
        {
            get { return m_nameFirstPoint; }
            set { m_nameFirstPoint = value; }
        }

        public String NAMEENDPOINT
        {
            get { return m_nameEndPoint; }
            set { m_nameEndPoint = value; }
        }

        #endregion

        #region Initialization

        public MyPath(String _str, String _strname, String _nameFirstPoint, String _nameEndPoint)
        {
            m_myPath = new Path();
            m_myPath.Name = _strname;
            m_nameFirstPoint = _nameFirstPoint;
            m_nameEndPoint = _nameEndPoint;
            m_myPath.Stroke = Brushes.Black;
            m_myPath.StrokeThickness = 2;
            m_myPath.Data = Geometry.Parse(_str);
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public class MyPathList
    {

        #region Fields

        private String m_str;

        private int m_selectPoint;

        private MyPointList m_listMyPoint;

        private int m_count;

        private List<MyPath> m_listMyPath;

        private IInputElement m_iInputElement;

        private String m_tempNameFirstPoint;

        private String m_tempNameEndPoint;

        #endregion

        #region Properties

        public String STR
        {
            get { return m_str; }
            set { m_str = value; }
        }

        public int SELECTPOINT
        {
            get { return m_selectPoint; }
            set { m_selectPoint = value; }
        }

        public int COUNT
        {
            get { return m_count; }
            set { m_count = value; }
        }

        public List<MyPath> LISTMYPATH
        {
            get { return m_listMyPath; }
            set { m_listMyPath = value; }
        }

        public int getAutomatic()
        {
            int i = 0;
            foreach (MyPath path in m_listMyPath)
            {
                string str = "Path";
                if ((str + i.ToString()) == path.MYPATH.Name)
                {
                    ++i;
                }
            }
            return i;
        }

        #endregion

        #region Initialization

        public MyPathList(MyPointList _myPointList, IInputElement _iInPutElement)
        {
            m_selectPoint = -1;
            m_str = null;
            m_count = 0;
            m_listMyPoint = _myPointList;
            m_iInputElement = _iInPutElement;
            m_listMyPath = new List<MyPath>();
            m_tempNameFirstPoint = null;
            m_tempNameEndPoint = null;
        }

        #endregion

        #region Process

        public void ResetCollectData()
        {
            m_count = 0;
            m_str = null;
            m_selectPoint = -1;
        }

        public int IsClickMyPoint(int delta)
        {
            int index = 0;
            foreach (MyPoint testPoint in m_listMyPoint.LISTMYPOINT)
            {
                Point currentPositionMouse = Mouse.GetPosition(m_iInputElement);
                if (currentPositionMouse.X >= (testPoint.ELL.X - delta) &&
                    currentPositionMouse.X <= (testPoint.ELL.X + delta) &&
                    currentPositionMouse.Y >= (testPoint.ELL.Y - delta) &&
                    currentPositionMouse.Y <= (testPoint.ELL.Y + delta))
                {
                    return index;
                }
                ++index;
            }

            return -1;

        }

        public int SearchIndex(String _str)
        {
            int index = 0;
            foreach (MyPath path in m_listMyPath)
            {
                if (_str == path.MYPATH.Name)
                {
                    return index;
                }

                ++index;
            }

            return -1;
        }

        public int CollectDataLine()
        {
            int index = IsClickMyPoint(10);

            if (m_count == 0 && index != -1)
            {
                m_str += "M" + m_listMyPoint.LISTMYPOINT[index].ELL.X.ToString()
                       + "," + m_listMyPoint.LISTMYPOINT[index].ELL.Y.ToString() + " ";
                m_selectPoint = index;
                m_tempNameFirstPoint = m_listMyPoint.LISTMYPOINT[index].ELL.Name;

                return 1;
            }

            if (m_count == 1)
            {
                if (index == -1)
                {
                    Point currentPositionMouse = Mouse.GetPosition(m_iInputElement);
                    m_str += "Q" + currentPositionMouse.X.ToString() + ","
                           + currentPositionMouse.Y.ToString() + " ";

                    return 2;
                }
                if (index != m_selectPoint)
                {
                    m_str += "L" + m_listMyPoint.LISTMYPOINT[index].ELL.X.ToString()
                       + "," + m_listMyPoint.LISTMYPOINT[index].ELL.Y.ToString();
                    m_tempNameEndPoint = m_listMyPoint.LISTMYPOINT[index].ELL.Name;

                    return 3;
                }
            }

            if (m_count == 2 && index != -1 && index != m_selectPoint)
            {
                m_str += m_listMyPoint.LISTMYPOINT[index].ELL.X.ToString()
                        + "," + m_listMyPoint.LISTMYPOINT[index].ELL.Y.ToString();
                m_tempNameEndPoint = m_listMyPoint.LISTMYPOINT[index].ELL.Name;

                return 3;
            }

            ResetCollectData();
            return 0;
        }

        public int CollectDataCircle()
        {
            int index = IsClickMyPoint(10);

            if (m_count == 0 && index != -1)
            {
                m_str += "M" + m_listMyPoint.LISTMYPOINT[index].ELL.X.ToString()
                       + "," + m_listMyPoint.LISTMYPOINT[index].ELL.Y.ToString() + " ";
                m_selectPoint = index;
                m_tempNameFirstPoint = m_listMyPoint.LISTMYPOINT[index].ELL.Name;

                return 1;
            }

            if (m_count == 1 && index == -1)
            {
                Point currentPositionMouse = Mouse.GetPosition(m_iInputElement);
                m_str += "C" + currentPositionMouse.X.ToString() + ","
                        + currentPositionMouse.Y.ToString() + " ";

                return 2;
            }

            if (m_count == 2 && index == -1)
            {
                Point currentPositionMouse = Mouse.GetPosition(m_iInputElement);
                m_str += currentPositionMouse.X.ToString() + ","
                        + currentPositionMouse.Y.ToString() + " ";

                return 3;
            }

            if (m_count == 3 && index == m_selectPoint)
            {
                m_str += m_listMyPoint.LISTMYPOINT[index].ELL.X.ToString()
                        + "," + m_listMyPoint.LISTMYPOINT[index].ELL.Y.ToString();
                m_tempNameEndPoint = m_listMyPoint.LISTMYPOINT[index].ELL.Name;

                return 4;
            }

            ResetCollectData();
            return 0;
        }

        public Path AddMyLineToMyList()
        {
            m_count = CollectDataLine();

            if (m_count == 3)
            {
                MyPath line = new MyPath(m_str, "Path" + getAutomatic().ToString(), m_tempNameFirstPoint, m_tempNameEndPoint);
                m_listMyPath.Add(line);
                ResetCollectData();
                return line.MYPATH;
            }
            return null;
        }

        public Path AddMyCircleToMyList()
        {
            m_count = CollectDataCircle();

            if (m_count == 4)
            {
                MyPath circle = new MyPath(m_str, "Path" + getAutomatic().ToString(), m_tempNameFirstPoint, m_tempNameEndPoint);
                m_listMyPath.Add(circle);
                ResetCollectData();
                return circle.MYPATH;
            }

            return null;
        }

        #endregion

    }
}
